package com.loc;

import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.net.wifi.WifiInfo;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import androidx.core.view.MotionEventCompat;
import com.loc.da;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import javax.crypto.KeyGenerator;
import kotlin.UByte;

/* compiled from: CollectionManager.java */
/* loaded from: classes2.dex */
public final class es implements em {
    private static long k;
    Context a;
    dw d;
    bv e;
    private Handler g;
    private LocationManager h;
    private a i;
    private ArrayList<dc> f = new ArrayList<>();
    fh b = null;
    fb c = null;
    private volatile boolean j = false;

    /* JADX INFO: Access modifiers changed from: package-private */
    /* compiled from: CollectionManager.java */
    /* loaded from: classes2.dex */
    public static class a implements LocationListener {
        private es a;

        a(es esVar) {
            this.a = esVar;
        }

        final void a() {
            this.a = null;
        }

        final void a(es esVar) {
            this.a = esVar;
        }

        @Override // android.location.LocationListener
        public final void onLocationChanged(Location location) {
            try {
                es esVar = this.a;
                if (esVar != null) {
                    esVar.a(location);
                }
            } catch (Throwable unused) {
            }
        }

        @Override // android.location.LocationListener
        public final void onProviderDisabled(String str) {
        }

        @Override // android.location.LocationListener
        public final void onProviderEnabled(String str) {
        }

        @Override // android.location.LocationListener
        public final void onStatusChanged(String str, int i, Bundle bundle) {
        }
    }

    /* JADX INFO: Access modifiers changed from: package-private */
    /* compiled from: CollectionManager.java */
    /* loaded from: classes2.dex */
    public class b extends cs {
        private int b;
        private Location c;

        b(int i) {
            this.b = 0;
            this.b = i;
        }

        b(es esVar, Location location) {
            this(1);
            this.c = location;
        }

        private void b() {
            try {
                fv.a();
                if (this.c != null && es.this.j) {
                    if (gb.m(es.this.a)) {
                        fv.a();
                        return;
                    }
                    Bundle extras = this.c.getExtras();
                    int i = extras != null ? extras.getInt("satellites") : 0;
                    if (gb.a(this.c, i)) {
                        return;
                    }
                    if (es.this.b != null && !es.this.b.s) {
                        es.this.b.f();
                    }
                    ArrayList<eg> a = es.this.b.a();
                    List<dz> a2 = es.this.c.a();
                    da.a aVar = new da.a();
                    ef efVar = new ef();
                    efVar.i = this.c.getAccuracy();
                    efVar.f = this.c.getAltitude();
                    efVar.d = this.c.getLatitude();
                    efVar.h = this.c.getBearing();
                    efVar.e = this.c.getLongitude();
                    efVar.j = this.c.isFromMockProvider();
                    efVar.a = this.c.getProvider();
                    efVar.g = this.c.getSpeed();
                    efVar.l = (byte) i;
                    efVar.b = System.currentTimeMillis();
                    efVar.c = this.c.getTime();
                    efVar.k = this.c.getTime();
                    aVar.a = efVar;
                    aVar.b = a;
                    WifiInfo c = es.this.b.c();
                    if (c != null) {
                        aVar.c = eg.a(fe.a(c));
                    }
                    aVar.d = fh.z;
                    aVar.f = this.c.getTime();
                    aVar.g = (byte) o.i(es.this.a);
                    aVar.h = o.n(es.this.a);
                    aVar.e = es.this.b.k();
                    aVar.j = gb.a(es.this.a);
                    aVar.i = a2;
                    dc a3 = dw.a(aVar);
                    if (a3 == null) {
                        return;
                    }
                    synchronized (es.this.f) {
                        es.this.f.add(a3);
                        if (es.this.f.size() >= 5) {
                            es.this.e();
                        }
                    }
                    es.this.d();
                }
            } catch (Throwable th) {
                ft.a(th, "cl", "coll");
            }
        }

        private void c() {
            fv.a();
            if (gb.m(es.this.a)) {
                fv.a();
                return;
            }
            bl blVar = null;
            try {
                long unused = es.k = System.currentTimeMillis();
                if (es.this.e.f.c()) {
                    blVar = bl.a(new File(es.this.e.a), es.this.e.b);
                    ArrayList arrayList = new ArrayList();
                    byte[] f = es.f();
                    if (f == null) {
                        try {
                            blVar.close();
                            return;
                        } catch (Throwable unused2) {
                            return;
                        }
                    }
                    List b = es.b(blVar, es.this.e, arrayList, f);
                    if (b != null && b.size() != 0) {
                        es.this.e.f.a(true);
                        if (dw.a(x.b(dw.a(fi.a(f), p.b(f, dw.a(), x.c()), b)))) {
                            es.b(blVar, arrayList);
                        }
                    }
                    try {
                        blVar.close();
                        return;
                    } catch (Throwable unused3) {
                        return;
                    }
                }
                if (blVar != null) {
                    try {
                        blVar.close();
                    } catch (Throwable unused4) {
                    }
                }
            } catch (Throwable th) {
                try {
                    av.b(th, "leg", "uts");
                    if (blVar != null) {
                        try {
                            blVar.close();
                        } catch (Throwable unused5) {
                        }
                    }
                } catch (Throwable th2) {
                    if (blVar != null) {
                        try {
                            blVar.close();
                        } catch (Throwable unused6) {
                        }
                    }
                    throw th2;
                }
            }
        }

        @Override // com.loc.cs
        public final void a() {
            int i = this.b;
            if (i == 1) {
                b();
            } else if (i == 2) {
                c();
            } else if (i == 3) {
                es.this.g();
            }
        }
    }

    /* JADX INFO: Access modifiers changed from: package-private */
    public es(Context context) {
        this.a = null;
        this.a = context;
        bv bvVar = new bv();
        this.e = bvVar;
        cb.a(this.a, bvVar, at.k, 100, 1024000, "0");
        bv bvVar2 = this.e;
        int i = fs.g;
        boolean z = fs.e;
        int i2 = fs.f;
        bvVar2.f = new cn(context, i, "kKey", new cl(context, z, i2, i2 * 10, "carrierLocKey"));
        this.e.e = new be();
    }

    private static int a(byte[] bArr) {
        return ((bArr[0] & UByte.MAX_VALUE) << 24) | (bArr[3] & UByte.MAX_VALUE) | ((bArr[2] & UByte.MAX_VALUE) << 8) | ((bArr[1] & UByte.MAX_VALUE) << 16);
    }

    private static byte[] a(int i) {
        try {
            KeyGenerator keyGenerator = KeyGenerator.getInstance("AES");
            if (keyGenerator == null) {
                return null;
            }
            keyGenerator.init(i);
            return keyGenerator.generateKey().getEncoded();
        } catch (Throwable unused) {
            return null;
        }
    }

    /* JADX INFO: Access modifiers changed from: private */
    /* JADX WARN: Code restructure failed: missing block: B:19:0x0039, code lost:
        if (r9 != null) goto L26;
     */
    /* JADX WARN: Code restructure failed: missing block: B:28:0x004d, code lost:
        if (r9 == null) goto L23;
     */
    /* JADX WARN: Code restructure failed: missing block: B:54:0x00ce, code lost:
        if (r9 == null) goto L87;
     */
    /* JADX WARN: Code restructure failed: missing block: B:55:0x00d0, code lost:
        r9.close();
     */
    /* JADX WARN: Code restructure failed: missing block: B:69:0x00eb, code lost:
        if (r9 == null) goto L87;
     */
    /* JADX WARN: Code restructure failed: missing block: B:83:0x011d, code lost:
        return r1;
     */
    /* JADX WARN: Removed duplicated region for block: B:131:0x0116 A[SYNTHETIC] */
    /* JADX WARN: Removed duplicated region for block: B:97:0x0104 A[EXC_TOP_SPLITTER, SYNTHETIC] */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
        To view partially-correct code enable 'Show inconsistent code' option in preferences
    */
    public static java.util.List<com.loc.dc> b(com.loc.bl r17, com.loc.bv r18, java.util.List<java.lang.String> r19, byte[] r20) {
        /*
            Method dump skipped, instructions count: 295
            To view this dump change 'Code comments level' option to 'DEBUG'
        */
        throw new UnsupportedOperationException("Method not decompiled: com.loc.es.b(com.loc.bl, com.loc.bv, java.util.List, byte[]):java.util.List");
    }

    /* JADX INFO: Access modifiers changed from: private */
    public static void b(bl blVar, List<String> list) {
        if (blVar != null) {
            try {
                for (String str : list) {
                    blVar.c(str);
                }
                blVar.close();
            } catch (Throwable th) {
                av.b(th, "aps", "dlo");
            }
        }
    }

    private static byte[] b(int i) {
        return new byte[]{(byte) ((i >> 24) & 255), (byte) ((i >> 16) & 255), (byte) ((i >> 8) & 255), (byte) (i & 255)};
    }

    private static byte[] c(int i) {
        return new byte[]{(byte) ((i & MotionEventCompat.ACTION_POINTER_INDEX_MASK) >> 8), (byte) (i & 255)};
    }

    static /* synthetic */ byte[] f() {
        return a(128);
    }

    /* JADX INFO: Access modifiers changed from: private */
    public void g() {
        try {
            fv.a();
            if (gb.m(this.a)) {
                fv.a();
                return;
            }
            ArrayList<dc> arrayList = this.f;
            if (arrayList != null && arrayList.size() != 0) {
                ArrayList arrayList2 = new ArrayList();
                synchronized (this.f) {
                    arrayList2.addAll(this.f);
                    this.f.clear();
                }
                ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
                byte[] a2 = a(256);
                if (a2 == null) {
                    return;
                }
                byteArrayOutputStream.write(c(a2.length));
                byteArrayOutputStream.write(a2);
                Iterator it = arrayList2.iterator();
                while (it.hasNext()) {
                    dc dcVar = (dc) it.next();
                    byte[] b2 = dcVar.b();
                    if (b2.length >= 10 && b2.length <= 65535) {
                        byte[] b3 = p.b(a2, b2, x.c());
                        byteArrayOutputStream.write(c(b3.length));
                        byteArrayOutputStream.write(b3);
                        byteArrayOutputStream.write(b(dcVar.a()));
                    }
                }
                bw.a(Long.toString(System.currentTimeMillis()), byteArrayOutputStream.toByteArray(), this.e);
            }
        } catch (Throwable th) {
            ft.a(th, "clm", "wtD");
        }
    }

    @Override // com.loc.em
    public final el a(ek ekVar) {
        try {
            fn fnVar = new fn();
            fnVar.a(ekVar.b);
            fnVar.b(ekVar.a);
            fnVar.a(ekVar.d);
            bo.a();
            bu a2 = bo.a(fnVar);
            el elVar = new el();
            elVar.c = a2.a;
            elVar.b = a2.b;
            elVar.a = 200;
            return elVar;
        } catch (Throwable unused) {
            return null;
        }
    }

    /* JADX INFO: Access modifiers changed from: package-private */
    public final void a() {
        LocationManager locationManager;
        if (gb.m(this.a)) {
            fv.a();
            return;
        }
        try {
            a aVar = this.i;
            if (aVar != null && (locationManager = this.h) != null) {
                locationManager.removeUpdates(aVar);
            }
            a aVar2 = this.i;
            if (aVar2 != null) {
                aVar2.a();
            }
            if (this.j) {
                g();
                this.b.a((es) null);
                this.c.a((es) null);
                this.c = null;
                this.b = null;
                this.g = null;
                this.j = false;
            }
        } catch (Throwable th) {
            ft.a(th, "clm", "stc");
        }
    }

    public final void a(Location location) {
        try {
            Handler handler = this.g;
            if (handler != null) {
                handler.post(new b(this, location));
            }
        } catch (Throwable th) {
            av.b(th, "cl", "olcc");
        }
    }

    public final void a(fb fbVar, fh fhVar, Handler handler) {
        LocationManager locationManager;
        fv.a();
        if (this.j || fbVar == null || fhVar == null || handler == null) {
            return;
        }
        if (gb.m(this.a)) {
            fv.a();
            return;
        }
        this.j = true;
        this.c = fbVar;
        this.b = fhVar;
        fhVar.a(this);
        this.c.a(this);
        this.g = handler;
        try {
            if (this.h == null && handler != null) {
                this.h = (LocationManager) this.a.getSystemService("location");
            }
            if (this.i == null) {
                this.i = new a(this);
            }
            this.i.a(this);
            a aVar = this.i;
            if (aVar != null && (locationManager = this.h) != null) {
                locationManager.requestLocationUpdates("passive", 1000L, -1.0f, aVar);
            }
            if (this.d == null) {
                dw dwVar = new dw("6.4.2", l.f(this.a), "S128DF1572465B890OE3F7A13167KLEI", l.c(this.a), this);
                this.d = dwVar;
                dwVar.a(o.k()).b(o.f(this.a)).c(o.a(this.a)).d(o.e(this.a)).e(o.n()).f(o.f()).g(Build.MODEL).h(Build.MANUFACTURER).i(Build.BRAND).a(Build.VERSION.SDK_INT).j(Build.VERSION.RELEASE).a(eg.a(o.h())).k(o.h());
                dw.b();
            }
        } catch (Throwable th) {
            ft.a(th, "col", "init");
        }
    }

    public final void b() {
        try {
            fv.a();
            Handler handler = this.g;
            if (handler != null) {
                handler.post(new Runnable() { // from class: com.loc.es.1
                    @Override // java.lang.Runnable
                    public final void run() {
                        try {
                            if (es.this.d == null || es.this.b == null) {
                                return;
                            }
                            dw.b(es.this.b.a());
                        } catch (Throwable th) {
                            ft.a(th, "cl", "upwr");
                        }
                    }
                });
            }
        } catch (Throwable th) {
            ft.a(th, "cl", "upw");
        }
    }

    public final void c() {
        fb fbVar;
        try {
            fv.a();
            if (this.d == null || (fbVar = this.c) == null) {
                return;
            }
            dw.a(fbVar.a());
        } catch (Throwable th) {
            ft.a(th, "cl", "upc");
        }
    }

    public final void d() {
        try {
            if (gb.m(this.a)) {
                fv.a();
            } else if (System.currentTimeMillis() - k < 60000) {
            } else {
                cr.a().b(new b(2));
            }
        } catch (Throwable unused) {
        }
    }

    public final void e() {
        try {
            cr.a().b(new b(3));
        } catch (Throwable unused) {
        }
    }
}
